<?php
class RedisModel
{
    #redis   key
    //存放队列
    private $sSetKestrel         = 'SSet:kestrel:';
    private $sSetFriends         = 'SSet:friends:';	
    private $sSetFollows         = 'SSet:followers:';
    private $sSetBlocked         = 'SSet:blocked:';
    private $sSetMeLikes         = 'SSet:likes:';
    private $sSetRecomment	     = 'SSet:recomuser:type:';
    private $sSetRecomTime	     = 'SSet:recomment:updatetime';
    private $sSetBoardUsers	     = 'SSet:board:users:';
    private $sSetBoardActiveTime = 'SSet:board:users:active:uptime';
    private $sSetHotDiary        = 'SSet:hotdiary:';
    private $sSetHotDiaryAll     = 'SSet:hotdiary:all';
    private $sSetChatGroup	 = 'SSet:chatgroup:';
    private $sSetRecomendDianDiForU	 = 'SSet:recddfu:all';
    private $sSetRecommendTopic	 = 'SSet:RecommendTopic:all';
    private $sSetHotTypeTopic	 = 'SSet:hotttc:';
    private $sSetSubscription   = 'SSet:subscription';
    private $sSetShieldList     = 'SSet:shieldList:';
    
    private $listAdminManage     = 'List:manage:admins';
    private $listSquare          = 'List:diary:square:';          
    private $listUserTimeLine    = 'List:user:timeline:';
    private $listCommentByMe     = 'List:user:combyme:';
    private $listCommentToMe     = 'List:user:comtome:';
    private $listMentions        = 'List:user:mentions:';
    private $listCommentions     = 'List:user:commentions:';
    private $listUserFeedTimeLine    = 'List:user:feed:';
    private $listUserFeedMeTimeLine  = 'List:user:feedme:';
    private $listPushBaidu       = 'List:push:baidu';

    //匿名
    private $listAnonymous          = 'List:anonymous:new:';
    private $sSetRecomendAnonymous	 = 'SSet:recays:all';
    private $sSetOfficialAnonymous   = 'SSet:recays:ofc';
    private $sSetCheckAnonyComment	 = 'SSet:LastComment:bodyId:';

    // 日记
    private $hashDiaryCache      = 'Hash:diary:';
    private $hashDiaryCount      = 'Hash:diary:count:';
    private $sKeysDiaryCache     = 'SSet:diary:cache';
    private $hashHotComment      = 'H:d:h:c:';
    private $sSetHotCommentList  = 'S:d:h:c:L';

    // 动态
    private $hashFeedCache      = 'Hash:feed:';
    private $sKeysFeedCache     = 'SSet:feed:cache';
    
    // 公告
    private $sKeysAnnounceCache = 'S:announce'; // 存放公告的set列表
    private $sKeysAnnounceCount = 'S:k:announce';// 统计公告计数
    
    // 用户
    private $hashUserCache       = 'Hash:user:';
    private $hashUserSetting     = 'Hash:user:set:';
    private $hashUserCount       = 'Hash:user:count:';
    private $sKeysUserCache      = 'SSet:user:cache';
    private $sKeysUserCount      = 'SSet:user:count';
    private $hashUserStatus       = 'Hash:user:status:';
    private $sKeysUserStatus      = 'SSet:user:status';
    private $hashUserFeedStatus   = 'Hash:user:feed:';
    private $sKeysUserFeedStatus  = 'SSet:user:feed';
    private $hashUserFeedMeStatus   = 'Hash:user:feedme:';
    private $sKeysUserFeedMeStatus  = 'SSet:user:feedme';
    private $sKeysInitUserCount  = 'SSet:init_user:count';
    private $sKeysTimeLine       = 'SSet:usertimeline:cache';
    private $sKeysUserFeedTimeLine       = 'SSet:userfeed:cache';
    private $sKeysUserFeedMeTimeLine       = 'SSet:userfeedme:cache';
    private $sKeysFriends        = 'SSet:keys:sset_friends';
    private $sKeysRecommendTopic        = 'SSet:keys:sKeysRecommendTopic';
    private $sKeysFollowers      = 'SSet:keys:sset_followers';
    private $sKeysBlocked        = 'SSet:keys:sset_blocked';
    private $hashUserFlushTimeCache    = 'Hash:user:ft:';
    private $sKeysUserFlushTimeStatus  = 'SSet:user:ft';
    private $hashRobotMsgCache = 'Hash:robotMsg:';
    private $sSetRobotMsg = 'S:R:msg';
    private $sKeysGuestFindAccount = 'SSet:user:cfe:';

    // token
    private $hashTokenCache      = 'Hash:token:';
    private $sKeysToken          = 'SSet:usertoken:cache';
    
    // 评论
    private $hashCommentCache    = 'Hash:comment:';
    private $sRemoveUserComment  = 'SSet:remove_user_comment';
    
    // 备注
    private $hashRmaskCache      = 'hash:rmask:';
    private $sKeysRmask          = 'SSet:remask';
    
    // 推送设置
    private $hashPushCache      = 'Hash:push:';
    private $sKeysPush          = 'SSet:push';
    
    // 禁言
    private $hashShutUpUserCache = 'hash:shutup_user:';
    
    // 圈子
    private $hashGroupCache      = 'Hash:group:';
    private $hashTopicCache      = 'Hash:topic:';
    private $sKeysGroup          = 'SSet:keys:sset_group';
    private $chatListCtimeCache  = 'Hash:gctime:';
    private $sKeysChatListCtime  = 'SSet:keys:chat_ctime';
    private $sSetViewTopic       = 'SSet:user_view_topic:';
    private $sSetOfficialGroup   = 'SSet:O:group';      //官方圈子列表
    private $sSetRecommendGroupList    = 'SSet:C:group';     //推荐列表
    private $sSetTopGroupCategory = 'SSet:T:group:';     //分类置顶圈子
    private $sSetGroupPause      = 'SSet:group:p';
    private $sSetCategoryGroup   =  'SSet:C:G:';
    
    // 非圈子数据库字段计数缓存存储
    private $hashGroupNodbCache	 = 'Hash:group_count:';
    
    // 圈子中的用户
    private $hashUserGroupCache  = 'Hash:group:user:';
    private $sKeysUserGroupCache = 'SSet:group_user:uids';
    private $sKeysGroupUsers     = 'SSet:group_gids:';
    private $sKeysGroupBlockUsers= 'SSet:group_block_gids:';

    // 用户&信纸
    private $hashPaperCache      = 'Hash:paper:';
    private $sKeysPaper          = 'SSet:keys:sset_paper';
    
    // 话题  hashTopicCache
    private $sSetStickTopic      = 'SSet:sticktopic:';
    private $sKeysTopic          = 'SSet:keys:sset_topic';
    private $sKeysStickTopic     = 'SSet:keys:sset_StickTopic';
    private $ckTopic             = 'topic:ck_user:';
    private $sSetFollowTopic     = 'SSet:foltc:user:';//用户关注话题
    
    // 群组
    private $hashChatMessageCache= 'Hash:chat_gid_id:';
    private $listGroupChat       = 'List:chat:group:';
    private $sSetGroupChatMessage= 'SSet:group_chat_message';
    private $sKeysChatGroup      = 'SSet:chat_group:cache';
    private $sSetUserChatGroups	 = 'S:u:cg:'; // 用户加入的群组id的set集合
    private $sSetUserChatGroupKeys = 'S:u:cg:keys'; // 用于统计上面的key
    
    // 聊天室
    private $hashChatRoom           = 'H:cr:'; // Hsah:chatRoom: 存储聊天室详情
    private $sKeysChatRoom          = 'S:k:cr'; // sSet:keys:chatRoom 统计聊天室hash的key(聊天室个数)
    private $sSetChatRoom           = 'S:cr:'; // sSet:chatRoom: 存储聊天室用户列表
    private $sSetChatRoomCategory   = 'S:cr:c:'; // sSet:chatRooms:category: 存储聊天室分类下的聊天室列表
    private $sKeysChatRoomCategory  = 'S:k:crc'; // sSet:keys:chatRoomCategory 统计聊天室分类key
    
    //LBS
    private $hashCity           = 'H:city'; // Hash:city: 城市详情
    private $sKeysCity          = 'S:k:city'; // sSet:keys:city 统计城市hash的key数
    private $sSetDiaryiesOfCity = 'S:d:ct:'; // sSet:diary:city: 存储城市日记列表
    private $sKeysDiariesOfCity = 'S:k:d:ct'; // sSet:keys:diary:city 统计城市set列表的key

    //个性装扮
    private $setDisguise = "set:disguise";
    //手账

    private $planner = array(
        'Planner-Tag'=>'S:M:tag',
        'Planner-Paper'=>'S:M:paper',
        'Planner-Stickers'=>'S:M:sticker',
        'Planner-Brushs'=>'S:M:brush',
        'planner-Tem'=>'S:M:tem',
        'planner-Frame'=>'S:M:frame',
        'planner-Plugin'=>'S:M:plugin', 
        'planner-Bootmgr'=>'S:M:bootmgr'
    );

    //文章定时发送参数
    private $hashArticleDiaryList = 'H:A:D:L';   //文章定时发送参数列表

    //diary topic
    private $sSetTopicDiarys        = 'S:t:ds:'; // sSet:topic:diarys: 存储点滴话题日记列表
    private $sKeysTopicDiarys       = 'S:k:t:ds'; // sSet:keys:topic:diarys 统计城点滴话题日记set列表的key
    private $sSetHotTopics          = 'S:tps:hot'; // 热门话题列表
    private $sSetHotOfficialTopics	= 'S:otps:hot'; //官方设置的的热门话题热度
    private $hashDiaryTopic     	= 'H:topic:';//话题信息
    private $sSetDiaryTopic         = 'S:d:topic';//话题信息key的统计
    private $sSetTopicHotDiarys     = 'S:t:hds:';//sSet:topic:hot:diarys: 存储话题下的热门点滴列表
    private $sKeysTopicHotDiarys     = 'S:k:t:hds';//sSet:keys:topic:hot:diarys: 统计key

    //写日记判断是否重复发送
    private $ckDiary             = 'diary:ck_user:';
    private $ckDiaryComment      = 'comment:ck:u:';
    private $ckCommnet           = 'comment:ck_user:';
    
    // 轮询  hashPollingCache
    private $sKeysPolling        = 'SSet:keys:sset_polling';
    
    // 置顶  sSetMeLikes
    private $sKeysMeLikes        = 'SSet:keys:sset_MeLikes';
    private $operationCount  	 = 'Hash:stat:';
    
    // 计数统计错误日志
    private $hashStatErrorCount  = 'Hash:error_stat';

    //手账资源分类缓存列表
    private $sKeysPlannerCache = 'S:planner:';
    private $sKeysPlannerCount = 'S:k:planner';

    #运营统计
    private $stat_rebuildTimeline    = "rebuild-timeout";
    private $redisTimeOut    = "timeout";
    private $redisPushUsec   = "push-usec";
    private $redisPopUsec    = "pop-usec";
    private $redisGetUsec    = "get-usec";
    private $redisSetUsec    = "set-usec";
    private $redisLtrimUsec  = "ltrim-usec";
    private $redisLlenUsec   = "llen-usec";
    private $redisTimeLineHit    = "stat-timeline-hit";
    private $redisTimeLineMiss   = "timeline-miss";
    private $redisHashDiaryHit   = "diary-hit";
    private $redisHashDiaryMiss  = "diary-miss";
    private $redisHashUserHit    = "user-hit";
    private $redisHashUserMiss   = "user-miss";
    private $redisHashCommentHit = "comment-hit";
    private $redisHashCommentMiss= "comment-miss";
    private $redisHashTokenHit   = "token-hit";
    private $redisHashTokenMiss  = "token-miss";


    private $highQualitySubscription = 'Z:good:sub';

    private $redis;
    
    
    public function __construct(){
        $this->redis    =    RedisPool::get('FS1');
    }
    

    public function getHighQualitySubscription($id, $length)
	{
		$lists = $this->redis->zRevRange($this->highQualitySubscription, 0, -1);
		$start=0;
		if (intval($id) > 0){
			$start = array_search($id, $lists);
			$start ++;
		}
		$result=array_slice($lists, $start, $length);

		return  $result;
	}

    /**
     * 增加hash缓存
     * @param unknown $key
     * @param unknown $data
     */
    public function addHash($pre_key, $ckey, $id, $data){
    	$result = $this->redis->hMset($pre_key.$id, $data);
    
    	$this->redis->expire($pre_key.$id, 604800);
    	$this->addKeysCount($ckey, $id);
    }
    
    /**
     * 获取hash缓存
     * @param unknown $gid
     * @param unknown $data
     */
    public function getHash($pre_key, $id){     	  
    	return $this->redis->hGetAll($pre_key.$id);		
    }
    
    /**
     * 删除hash缓存
     * @param unknown $key
     * @param unknown $id
     */
    public function delHash($pre_key, $id){
    	return $this->redis->delete($pre_key.$id);
    }
    
    /**
     * 获取缓存(Hash)
     * @param unknown $key
     * @param unknown $type
     * @return boolean
     */
    public function getMysqlDataHashCache($table, $primaryKey){
        $key    =    $this->generateHashKey($table, $primaryKey);
        return $this->redis->hGetAll($key);
    }
    
    /**
     * 设置缓存(Hash)
     * @param unknown $key
     * @param unknown $type
     * @param unknown $value
     * @return boolean|unknown
     */
    public function setMysqlDataHashCache($table, $primaryKey, $value){
        $keys   =    "SSet:keys:".$table;
        $key    =    $this->generateHashKey($table, $primaryKey);
        
        $result = $this->redis->hMset($key, $value);
        $this->redis->expire($key,172800);
        $this->addKeysCount($keys, $primaryKey);
        return $result;
    }
    
    /**
     * 删除缓存(Hash)
     * @param unknown $key
     * @param unknown $type
     * @return boolean
     */
    public function delMysqlDataHashCache($table, $primaryKey){
        $key    =    $this->generateHashKey($table, $primaryKey);
        return $this->redis->delete($key);
    }
    
    /**
     * 修改数字字段(增加或减少)
     * @param unknown $gid
     * @param unknown $type
     * @param number $num
     */
    public function setCount($table, $primaryKey, $type, $num=1){
        $key    =    $this->generateHashKey($table, $primaryKey);
        return $this->redis->hIncrBy($key, $type, $num);
    }
    
    /**
     * 组装key
     * @param unknown $key
     * @return boolean|string
     */
    private function generateHashKey($table, $primaryKey){
        $key    =    "Hash:Mysql:".$table.":".$primaryKey;
        return $key;
    }
    
    /**
     * 获取信纸或表情列表(可分类)
     * @param unknown $redisKey
     * @param unknown $sort
     * @param unknown $id
     * @param unknown $start
     * @param unknown $length
     * @return multitype:
     */
    public function getShopListData ($redisKey, $sort, $id, $start = 0, $length = 20) {
        // List:Shop-Paper:hot   List:Shop-Paper:new   List:Shop-Paper:free
        // List:Shop-Emotion:hot List:Shop-Emotion:new List:Shop-Emotion:free
        $key      = "List:".$redisKey.":".$sort;
        $id       = intval($id);
        $list     = $this->redis->lRange($key, 0, -1);

        if ($id > 0){
            $start = array_search($id, $list);
            $start ++;
        }
        return  array_slice($list, $start, $length);
    }
    
    /**
     * 添加信纸或表情进列表
     * @param unknown $redisKey
     * @param unknown $sort
     * @param unknown $id
     */
    public function addShopListData($redisKey, $sort, $id){
        $key = "List:".$redisKey.":".$sort;
        $this->redis->rPush($key, $id);
    }
    
    ###########  好友备注      ################
    /**
     * 添加好友备注缓存
     * @param unknown $uid
     * @param unknown $fUid
     * @param unknown $data
     */
    public function addRmaskCache($uid, $fUid, $data){
        $this->redis->hSet($this->hashRmaskCache.$uid, $fUid, $data);
        $this->addKeysCount($this->sKeysRmask, $uid);
    }
    /**
     * 获取好友的备注
     * @param unknown $uid
     */
    public function getRmaskCache($fUid,$uid){
        return $this->redis->hGet($this->hashRmaskCache.$fUid,$uid);
    }
    /**
     * 删除备注
     * @param unknown $uid
     * @param unknown $fUid
     */
    public function removeRmaskCache($uid,$fUid){
        return $this->redis->hDel($this->hashRmaskCache.$uid,$fUid);
    }
    
    ############## push信息 ################
    public function setUserPushInfoCache($uid, $data){
        $this->redis->hMset($this->hashPushCache.$uid, $data);
        $this->redis->expire($this->hashPushCache.$uid, 604800);
        $this->addKeysCount($this->sKeysPush, $uid);
    }
    
    public function getUserPushInfoCache($uid){
        return $this->redis->hGetAll($this->hashPushCache.$uid);
    }
    
    public function delUserPushInfoCache($uid){
        return $this->redis->delete($this->hashPushCache.$uid);
    }
    
    ############## 圈子  start    ################
    /**
     * 添加圈子缓存
     * @param unknown $gid
     * @param unknown $result
     */
    public function addGroup($gid, $result){
        $this->redis->hMset($this->hashGroupCache.$gid, $result);
        $this->redis->expire($this->hashGroupCache.$gid,172800);
	    $this->addKeysCount($this->sKeysGroup, $gid);
    }
    /**
     * 增加圈子话题数,成员数,今日新增话题数
     * @param unknown $gid
     * @param unknown $type
     * @param number $num
     */
    public function addGroupCount($gid,$type, $num=1){
        $this->redis->hIncrBy($this->hashGroupCache.$gid, $type, $num);
    }
    /**
     * 修改圈子缓存(单个字段)
     * @param unknown $gid
     * @param unknown $type
     * @param unknown $value
     */
    public function setGroupCount($gid,$type, $value){
    	$this->redis->hset($this->hashGroupCache.$gid, $type, $value);
    }
    /**
     * 修改圈子缓存
     * @param unknown $info
     */
    public function setGroup($gid,$info){
    	$this->redis->hMset($this->hashGroupCache.$gid,$info);
    }
    /**
     * 获取圈子信息
     * @param unknown $gid
     */
    public function getGroup($gid){
    	return $this->redis->hGetAll($this->hashGroupCache.$gid);
    }
    /**
     * 删除圈子
     * @param unknown $gid
     */
    public function delGroup($gid){
    	$this->redis->delete($this->hashGroupCache.$gid);
    }
    /**
     * 圈子非缓存字段存储
     * @param unknown $gid
     * @param unknown $values
     */
    public function setGroupNodbCache($gid,$values){
    	$this->redis->hMset($this->hashGroupNodbCache.$gid, $values);
    }

    /**
     * 圈子增加用户
     * @param $gid
     * @param $uid
     * @param int $time
     * @return mixed
     */
    public function addGroupUser($gid, $uid, $time = AC::SERVER_TIME){
        $count = (int)$this->getGroupUserNum($gid);
        if ($count > 500){
            $list = $this->getGroupUser($gid, 0, -1);
            $this->removeGroupUser($gid, end($list));
        }
        return $this->redis->zAdd($this->sKeysGroupUsers.$gid, $time, $uid);
    }

    //增加被移除用户
    public function addGroupBlackUser($gid, $uid, $time = AC::SERVER_TIME){
        return $this->redis->zAdd($this->sKeysGroupBlockUsers.$gid, $time, $uid);
    }
    //获取被移除用户
    public function getGroupBlockUser($gid) {
        return $this->redis->zRange($this->sKeysGroupBlockUsers.$gid, 0, -1);
    }

    /**
     * 官方圈子
     * @param $gid
     * @return mixed
     */
    public function addOfficialGroupCache($gid) {
        return $this->redis->zAdd($this->sSetOfficialGroup, $gid, $gid);
    }

    /**
     * 官方圈子
     * @param $start
     * @param $len
     * @return mixed
     */
    public function getOfficialGroupCache($start, $len) {
        return $this->redis->zRange($this->sSetOfficialGroup, $start, $len);
    }

    public function delOfficialGroupCache() {
        return $this->redis->zRemRangeByRank($this->sSetOfficialGroup, 0, -1);
    }

    /**
     * 加入精选
     * @param $gid
     * @param $score
     * @return mixed
     */
    public function addRecommendGroup($gid, $score){
        return $this->redis->zAdd($this->sSetRecommendGroupList, $score, $gid);
    }

    /**
     * 获取精选列表
     * @param $start
     * @param $len
     * @return mixed
     */
    public function getRecommendGroup($start, $len) {
        return $this->redis->zRevRange($this->sSetRecommendGroupList, $start, $len);
    }

    /**
     * 获取圈子排序值
     * @param $gid
     * @return mixed
     */
    public function getGroupScore($gid) {
        return $this->redis->zScore($this->sSetRecommendGroupList, $gid);
    }

    public function getTopGroups($category, $start=0, $len=-1) {
        return $this->redis->zRevRange($this->sSetTopGroupCategory . $category, $start, $len);
    }

    /**
     * 移除圈子用户
     * @param $gid
     * @param $uid
     * @return mixed
     */
    public function removeGroupUser($gid, $uid){
        return $this->redis->zRem($this->sKeysGroupUsers.$gid, $uid);
    }

    /**
     * 获取圈子用户
     * @param $gid
     * @param int $start
     * @param int $length
     * @return mixed
     */
    public function getGroupUser($gid, $start = 0, $length = -1){
        return $this->redis->zRevRange($this->sKeysGroupUsers.$gid, $start, $length);
    }

    /**
     * 获取圈子用户个数
     * @param $gid
     * @return mixed
     */
    public function getGroupUserNum($gid){
        return $this->redis->zCard($this->sKeysGroupUsers.$gid);
    }

    /**
     * 今日话题数自增1
     * @param $gid
     * @param $type
     * @param int $num
     */
    public function groupTodayNumIncrete($gid, $type, $num = 1){
        $this->redis->hIncrBy($this->hashGroupNodbCache.$gid, $type, $num);
    }
    /**
     * 读取圈子非缓存字段数据
     * @param  $gid
     */
    public function getGroupNodbCache($gid){
    	return $this->redis->hGetAll($this->hashGroupNodbCache.$gid);
    }
    /**
     * 添加用户在圈子中的缓存
     * @param  $uid
     * @param  $data
     */
    public function addGroupUserCache($uid, $data){
    	$this->redis->hMset($this->hashUserGroupCache.$uid, $data);
    	$this->addKeysCount($this->sKeysUserGroupCache, $uid);
    }
    /**
     * 获取用户在圈子中的缓存
     * @param  $uid
     */
    public function getUserGroupAllCache($uid){
    	return $this->redis->hGetAll($this->hashUserGroupCache.$uid);
    }

    public function addGroupListByCategory($category, $score, $gid){
        $count = $this->getCountGroupListByCategory($category);
        if($count > 300){
            $list = $this->getGroupRangeByCategory($category, 0, -1);
            $this->remGroupByCategory($category, $list[0]);
        }
        return $this->redis->zAdd($this->sSetCategoryGroup . $category, $score, $gid);
    }
    //倒序
    public function getGroupListByCategory($category, $start, $len){
        return $this->redis->zRevRange($this->sSetCategoryGroup . $category, $start, $len);
    }
    //正序
    public function getGroupRangeByCategory($category, $start, $len){
        return $this->redis->zRange($this->sSetCategoryGroup . $category, $start, $len);
    }

    public function getCountGroupListByCategory($category){
        return $this->redis->zCard($this->sSetCategoryGroup . $category);
    }

    public function remGroupByCategory($category, $gid){
        return $this->redis->zRem($this->sSetCategoryGroup . $category, $gid);
    }

    ############## 用户在圈子的缓存  end    ################
    
    ############## 信纸的缓存  start    ################
    /**
     * 信纸的缓存
     * @param unknown $pid
     * @param unknown $data
     */
    public function setDiaryPaperCache($pid, $data){
        $result = $this->redis->hMset($this->hashPaperCache.$pid, $data);
        $this->redis->expire($this->hashPaperCache.$pid,172800);
        $this->addKeysCount($this->sKeysPaper, $pid);
        return $result;
    }
    /**
     * 获取信纸缓存
     * @param unknown $pid
     */
    public function getPaperDetailByIdCache($pid){
        return $this->redis->hGetAll($this->hashPaperCache.$pid);
    }
    /**
     * 删除信纸缓存
     * @param unknown $pid
     */
    public function delPaperCacheById($pid){
        return $this->redis->delete($this->hashPaperCache.$pid);
    }
    /**
     * 购买量增1
     * @param unknown $pid
     * @param unknown $type
     * @param number $num
     */
    public function addSoldCountCache($pid, $type, $num = 1){
        return $this->redis->hIncrBy($this->hashPaperCache.$pid, $type, $num);
    }
    ############## 信纸的缓存  end    ################
    
    ############# 话题是否重复发送   ##################
    
    public function setTopicCheck($uid,$info){
        return  $this->redis->set($this->ckTopic.$uid, $info);
    }
    
    public function getTopicCheck($uid){
        return $this->redis->get($this->ckTopic.$uid);
    }
    
    /**
     * 添加话题置顶列表
     * @param unknown $gid
     * @param unknown $time
     */
    public function addTopicList($tid, $time = AC::SERVER_TIME){
        $this->redis->zAdd($this->sSetStickTopic, $time, $tid);
	    $this->addKeysCount($this->sKeysStickTopic, $tid);
    }
    /**
     * 获取话题置顶列表
     * @param unknown $start
     * @param number $length
     */
    public function getTopicList($start, $length=20){
        return  $this->redis->zRevRange($this->sSetStickTopic, $start, $length);
    }
    /**
     * 删除话题置顶列表
     * @param unknown $tid
     */
    public function removeTopicInfo($tid){
        $this->redis->zRem($this->sSetStickTopic, $tid);
    }
    /**
     * 搜索话题置顶列表
     * @param unknown $tid
     */
    public function findStickTopic($tid){
        return $this->redis->zScore($this->sSetStickTopic, $tid);
    }
    /**
     * 添加话题
     * @param unknown $tid
     * @param unknown $result
     */
    public function addTopic($tid, $result){
        $this->redis->hMset($this->hashTopicCache.$tid, $result);
    	$this->redis->expire($this->hashTopicCache.$tid,172800);
    	$this->addKeysCount($this->sKeysTopic, $tid);
    }
    /**
     * 自增/自减话题计数
     * @param unknown $tid
     * @param unknown $type
     * @param number $num
     */
    public function addTopicCount($tid, $type, $num=1){
        $result = $this->redis->hIncrBy($this->hashTopicCache.$tid, $type, $num);
    }
    /**
     * 获取话题
     * @param unknown $tid
     */
    public function getTopic($tid){
       return $this->redis->hGetAll($this->hashTopicCache.$tid);
    }
    /**
     * 删除话题
     * @param unknown $tid
     */    
    public function delTopic($tid){
        $this->redis->delete($this->hashTopicCache.$tid);
    }
    ############## 圈子  end      ################
    
    ############# 日记是否重复发送   ##################
    
    public function setDiaryCheck($uid,$info){
        return  $this->redis->set($this->ckDiary.$uid, $info);
    }
    
    public function getDiaryCheck($uid){
        return $this->redis->get($this->ckDiary.$uid);
    }
    
    public function setDiaryCommentCheck($uid, $connent){
        return  $this->redis->set($this->ckDiaryComment.$uid, $connent);
    }
    
    public function getDiaryCommentCheck($uid){
        return $this->redis->get($this->ckDiaryComment.$uid);
    }
    
    // 判断评论间隔时间
    public function getCommentCheck($uid){
        return $this->redis->get($this->ckCommnet.$uid);
    }
    
    public function setCommentCheck($uid, $info){
        return  $this->redis->set($this->ckCommnet.$uid, $info);
    }
    
    ############## 管理员列表   ###############################
    public function getAdminList(){
        $list    =    $this->redis->lRange($this->listAdminManage, 0, -1);
        return $list;
    }
    
    ############ 时间线 基本完成  ########################
	
    /**
     * 加入广场最新时间线,label是分类,每个分类800条时间线
     * @param unknown $bodyId
     * @param unknown $label
     */
    public function addSquareTimeline($category, $bodyId){
		$this->redis->lPush($this->listSquare.$category,$bodyId);
		$this->redis->lTrim($this->listSquare.$category,0,4999);
    }
    
    /**
     * 获取广场最新时间线
     * @param unknown $label
     * @param string $bodyId
     * @param string $start
     * @param string $length
     */
    public function getSquareTimeline($category, $bodyId='0', $start='0', $length='49'){
        $bodyId = intval($bodyId);
        $list   =    $this->redis->lRange($this->listSquare.$category, 0, -1);
	    if ($bodyId > 0){
	        $start = array_search($bodyId,$list) ;
	        $start ++;
	    }
	    return  array_slice($list, $start, $length);
    }
    /**
     * 删除广场最新的时间线
     * @param unknown $label
     * @param unknown $bodyId
     */
    public function delSquareTimeline($category, $bodyId){
        if (intval($category) > 0){
            $this->redis->lRem($this->listSquare.$category,$bodyId,0);
            $this->redis->lRem($this->listSquare.'0',$bodyId,0);
            return true;
        }
        $this->redis->lRem($this->listSquare.$category,$bodyId,0);
        return true;
    }
    
    public function countSquare($category){
        return $this->redis->lSize($this->listSquare.$category);
    }
    
    // ==============================话题 最新时间线====================================
    /**
     * 加入广场最新时间线,label是分类,每个分类800条时间线
     * @param unknown $bodyId
     * @param unknown $label
     */
    public function addTopicToSquareTimeline($category, $tid){
        $this->redis->lPush($this->listSquareOfTopic.$category, $tid);
        $this->redis->lTrim($this->listSquareOfTopic.$category, 0, 4999);
    }
    
    /**
     * 获取广场话题最新时间线
     * @param unknown $label
     * @param string $bodyId
     * @param string $start
     * @param string $length
     */
    public function getSquareTimelineOfTopic($category, $tid = 0, $start = 0, $length = 50){
        $tid    = intval($tid);
        $list   = $this->redis->lRange($this->listSquareOfTopic.$category, 0, -1);
        if ($tid > 0){
            $start = array_search($tid, $list);
            $start ++;
        }
        return  array_slice($list, $start, $length);
    }
    
	/**
	 * 增加用户关注时间线
	 * 用户关注的用户发布的日志id以及自己写的日志id
	 * @param $uid  key
	 * @param $bodyId   日志id
	 */
	public function addManyTimeLine($uid, $bodyId)/*{{{*/
	{
	    $this->redis->rPush($this->listUserTimeLine.$uid,$bodyId);
	    $this->redis->lTrim($this->listUserTimeLine.$uid,0,799);
	    $this->addKeysCount($this->sKeysTimeLine, $uid);
	    $this->addStat($this->redisPushUsec);
	    return true;
	}/*}}}*/
	
	/**
	 * 增加用户关注时间线
	 * 用户关注的用户发布的日志id以及自己写的日志id
	 * @param $uid  key
	 * @param $bodyId   日志id
	 */
	public function addTimeLine($uid, $bodyId)/*{{{*/
	{
	    $this->redis->lPush($this->listUserTimeLine.$uid,$bodyId);
	    $this->redis->lTrim($this->listUserTimeLine.$uid,0,799);
	    $this->addKeysCount($this->sKeysTimeLine, $uid);
	    $this->addStat($this->redisPushUsec);
	    return true;
	}/*}}}*/
	
	/**
	 * 清空列表
	 * @param unknown $uid
	 */
	public function delTimeLine($uid){
	    $this->redis->lTrim($this->listUserTimeLine.$uid,10000,800);
	}
	
	/**
	 * 删除日志时删除时间线中的bodyid
	 */
	public function lremTimeLine($uid,$bodyId){
	    $this->redis->lRem($this->listUserTimeLine.$uid,$bodyId,0);
	}
	/**
	 * 获取用户时间线信息,带分页功能
	 * @param uid key
	 * @param $start  分页开始
	 * @param $length 每页长度
	 * @param $bodyId 每页的最后一个
	 */
	public function getTimeLine($uid, $bodyId=0, $start=0,$length=20)/*{{{*/
	{
	    $bodyId = intval($bodyId);
	    $list    =    $this->redis->lRange($this->listUserTimeLine.$uid, 0, -1);
	    if ($bodyId > 0){
	        $start = array_search($bodyId,$list) ;
	        $start = $start + 1;
	    }
	    $this->addStat($this->redisPopUsec);
	    if (empty($start)){
	        $start = 0;
	    }
	    return  array_slice($list, $start, $length);
	}/*}}}*/
	
	/**
	 * 获取用户时间线信息,带分页功能
	 * @param uid key
	 * @param $start  分页开始
	 * @param $length 每页长度
	 * @param $feedId 每页的最后一个
	 */
	public function getUserFeedTimeline($uid, $feedId=0, $start=0, $length=20, $isMe = FALSE)/*{{{*/
	{
	    $feedkey = $this->listUserFeedTimeLine;
	    if($isMe){
	        $feedkey = $this->listUserFeedMeTimeLine;
	    }
	    $list    =    $this->redis->lRange($feedkey.$uid, 0, -1);
	    $feedId = intval($feedId);
	    if ($feedId > 0){
	        $start = array_search($feedId,$list) ;
	        $start = $start + 1;
	    }
	    $this->addStat($this->redisPopUsec);
	    if (empty($start)){
	        $start = 0;
	    }
	    return  array_slice($list, $start, $length);
	}/*}}}*/
	
	public function addPushToList($json_str){
	    $this->redis->lPush($this->listPushBaidu, $json_str);
	}
	
	/**
	* 官方公告列表进缓存
	* @param unknown $uid
	* @param unknown $feedId
	* @return boolean
	*/
	public function setAnnounceCache($feedId,$score){
	    if (empty($feedId)) return false;
		
	    $feedkey = $this->sKeysAnnounceCache;
	
	    $res = $this->redis->zAdd($feedkey, $score, $feedId);
	    $this->addKeysCount($this->sKeysAnnounceCount, $feedkey);
	    return $res;
	}
	
	/**
	 * 获取公告feedid列表
	 * @param number $start
	 * @param unknown $length
	 * @return unknown
	 */
	public function getAnnounceCache($id, $length){
	    $lists = $this->redis->zRevRange($this->sKeysAnnounceCache, 0, -1);
	    $start=0;
	    if (intval($id) > 0){
	        $start = array_search($id, $lists);
	        $start ++;
	    }
	    $result=array_slice($lists, $start, $length);
		 
	    return  $result;
	}
	
	/**
	 * 返回用户未访问的列表
	 * @param unknown $lastTime
	 */
	public function getNewAnnounceList($lastTime){
	    return $this->redis->zRangeByScore($this->sKeysAnnounceCache, $lastTime , AC::SERVER_TIME);
	}
	
	/**
	 * 缓存动态数据
	 * @param $uid
	 * @param $feedId
	 * @return array || false
	 */
	public function setFeedInfoCache($feedId, $result)
	{
	    if (empty($feedId) || empty($result)) return false;
	    $this->redis->hMset($this->hashFeedCache.$feedId, $result);
	    $this->redis->expire($this->hashFeedCache.$feedId, 864000);
	    $this->addKeysCount($this->sKeysFeedCache, $feedId);
	    return $result;
	}
	
	/**
	 * 返回动态缓存数据
	 * @param $uid
	 * @param $feedId
	 * @return array || false
	 */
	public function getFeedInfoCache($feedId)/*{{{*/
	{
	    $result = $this->redis->hGetAll($this->hashFeedCache.$feedId);
	     
	    if (!empty($result)) {
	        $this->addStat($this->redisHashDiaryHit);
	        return $result;
	    }
	    $this->addStat($this->redisHashDiaryMiss);
	    return false;
	}/*}}}*/
	
	/**
	 * 获取我喜欢的日记列表,传开始长度就可以了
	 * @param unknown $uid
	 * @param string $start
	 * @param string $length
	 */
	public function getMelikeList($uid, $start = 0, $length = 19){
	    return $this->redis->zRevRange($this->sSetMeLikes.$uid, $start, $length);
	}
	
	/**
	 * 判断是否已经喜欢某日记
	 * @param unknown $uid
	 * @param unknown $bodyId
	 */
	public function checkIsMeLike($uid, $bodyId){
	    return $this->redis->zScore($this->sSetMeLikes.$uid, $bodyId);
	}
	
	/**
	 * 增加喜欢列表
	 * @param unknown $uid
	 * @param unknown $bodyId
	 * @param unknown $time
	 */
	public function addMeLikesList($uid, $bodyId, $time = AC::SERVER_TIME){
	    $this->redis->zAdd($this->sSetMeLikes.$uid, $time, $bodyId);
	    $this->addKeysCount($this->sKeysMeLikes, $uid);
	}
	
	/**
	 * 删除一个喜欢日记
	 * @param unknown $uid
	 * @param unknown $bodyId
	 */
	public function delMeLikesList($uid, $bodyId){
	    $this->redis->zDelete($this->sSetMeLikes.$uid, $bodyId);
	}
	/**
	 * 判断日记是否喜欢
	 * @param unknown $uid
	 * @param unknown $bodyId
	 */
	public function checkIsLike($uid, $bodyId){
		return $this->redis->zScore($this->sSetMeLikes.$uid, $bodyId);
	}
	
	/**
	 * 统计喜欢日记数目
	 * @param unknown $uid
	 */
	public function countMeLikes($uid){
	    return $this->redis->zCount($this->sSetMeLikes.$uid);
	}
	
	/**
	 * 增加我评论的列表
	 * @param $uid
	 * @param $id  =  评论表里的id
	 * @return boolean
	 */
	public function addComments_by_me($uid, $hashId)/*{{{*/
	{
	    $this->redis->lPush($this->listCommentByMe.$uid,$hashId);
	    $this->redis->lTrim($this->listCommentByMe.$uid,0,799);
	    $this->addStat($this->redisPushUsec);
	    return true;
	}/*}}}*/
	
	/**
	 * 获取我评论的列表时间线信息,带分页功能
	 * @param uid key
	 * @param $start  分页开始
	 * @param $length 每页长度
	 * @param $bodyId 每页的最后一个
	 */
	public function getComments_by_me($uid, $hashId=0, $start=0,$length=20)/*{{{*/
	{
	    if ($start >= 19 && $hashId != 0){
	        $list    =    $this->redis->lRange($this->listCommentByMe.$uid, $start, $length);
	        $start = array_search($hashId,$list) ;
	        $length = $start + $length ;
	        $start = $start + 1;
	    }
	    $this->addStat($this->redisPopUsec);
	    return  $this->redis->lRange($this->listCommentByMe.$uid, $start, $length);
	}/*}}}*/
	
	/**
	 * 增加评论我的列表
	 * @param $uid
	 * @param $id  =  评论表里的id
	 * @return boolean
	 */
	public function addComments_to_me($uid, $hashId)/*{{{*/
	{
	    $this->redis->lPush($this->listCommentToMe.$uid,$hashId);
	    $this->redis->lTrim($this->listCommentToMe.$uid,0,799);
	    $this->addStat($this->redisPushUsec);
	    return true;
	}/*}}}*/
	
	/**
	 * 获取评论我的列表时间线信息,带分页功能
	 * @param uid key
	 * @param $start  分页开始
	 * @param $length 每页长度
	 * @param $bodyId 每页的最后一个
	 */
	public function getComments_to_me($uid, $hashId=0, $start=0,$length=20)/*{{{*/
	{
	    if ($start >= 19 && $hashId != 0){
	        $list    =    $this->redis->lRange($this->listCommentToMe.$uid, $start, $length);
	        $start = array_search($hashId,$list) ;
	        $length = $start + $length ;
	        $start = $start + 1;
	    }
	    $this->addStat($this->redisPopUsec);
	    return  $this->redis->lRange($this->listCommentToMe.$uid, $start, $length);
	}/*}}}*/
	
	############评论缓存      基本完成##########################
	
	/**
	 * 增加评论缓存
	 * @param $uid
	 * @param $bodyId
	 * @param $id
	 * @return boolean
	 */
	public function addCommentCache($hashId, $result)/*{{{*/
	{
        $this->redis->hMset($this->hashCommentCache.$hashId, $result);
        $this->redis->expire($this->hashCommentCache.$hashId,172800);
        $this->addStat($this->redisSetUsec);
        $this->addStat($this->redisHashCommentHit);
	        
	    return false;
	}/*}}}*/
	
	/**
	 * 根据hashId获取该条评论数据
	 * @param $hashId
	 * @return Ambigous <boolean, multitype:, unknown>
	 */
	public function commentHashId($hashId, $result)/*{{{*/
	{
	    $this->redis->hMset($this->hashCommentCache.$hashId, $result);
	    $this->redis->expire($this->hashCommentCache.$hashId,172800);
	    return true;
	}/*}}}*/
	
	/**
	 * 获取某条评论内容
	 * @param $hashId 经过用户名以及评论Id hash后的值
	 */
	public function getCommentOneCache($hashId)/*{{{*/
	{
	    $this->addStat($this->redisGetUsec);
	    $result = $this->redis->hGetAll($this->hashCommentCache.$hashId);
	    if (!empty($result)){
	        $this->addStat($this->redisHashCommentHit);
	        return $result;
	    }
	    $this->addStat($this->redisHashCommentMiss);
	    return false;
	}/*}}}*/
	
	/**
	 * 删除评论
	 * @param $uid
	 * @param $id
	 */
	public function delCommentCache($uid, $hashId)/*{{{*/
	{
	    
	    $this->redis->hDel($this->hashCommentCache.$hashId);
	    $this->redis->lRem($this->listCommentions.$uid, $hashId, 0);
	    $this->redis->lRem($this->listCommentByMe.$uid, $hashId, 0);
	    $this->redis->lRem($this->listCommentToMe.$uid, $hashId, 0);
	}/*}}}*/
	
	##########提及我日志和评论       基本完成#########################
	
	/**
	 * 提及我的评论列表
	 * @param $uid  = 被提及人
	 * @param $id   = 提及的评论id   hash算法后的
	 * @param $cUid = 提及了$uid
	 * @return boolean
	 */
	public function addCommentionsTimeLine($uid, $hashId)/*{{{*/
	{
	     $this->redis->lPush($this->listCommentions.$uid, $hashId);
	     $this->redis->lTrim($this->listCommentions.$uid,0,799);
	     $this->addStat($this->redisPushUsec);
	     return true;
	}/*}}}*/
	
	/**
	 * 获取提及我评论列表,带分页功能
	 * @param uid key
	 * @param $start  分页开始
	 * @param $length 每页长度
	 * @param $hashId 每页的最后一个
	 */
	public function getCommentionsTimeLine($uid, $hashId=0, $start=0,$length=20)/*{{{*/
	{
	    if ($start >= 19 && $hashId != 0){
	        $list    =    $this->redis->lRange($this->listCommentions.$uid, $start, $length);
	        $start = array_search($hashId,$list) ;
	        $length = $start + $length ;
	        $start++;
	    }
	    $this->addStat($this->redisPopUsec);
	    return  $this->redis->lRange($this->listCommentions.$uid, $start, $length);
	}/*}}}*/
	
	/**
	 * 提及我的日志列表 
	 * @param $uid
	 * @param $bodyId
	 * @return boolean
	 */
	public function addMentionsTimeLine($uid, $bodyId)/*{{{*/
	{
	    $this->redis->lPush($this->listMentions.$uid,$bodyId);
	    $this->redis->lTrim($this->listMentions.$uid,0,799);
	    $this->addStat($this->redisPushUsec);
	    return true;
	}/*}}}*/
	
	/**
	 * 获取提及我日志列表,带分页功能
	 * @param uid key
	 * @param $start  分页开始
	 * @param $length 每页长度
	 * @param $hashId 每页的最后一个
	 */
	public function getMentionsTimeLine($uid, $bodyId=0, $start=0,$length=20)/*{{{*/
	{
	    if ($start >= 19 && $bodyId != 0){
	        $list    =    $this->redis->lRange($this->listMentions.$uid, 0, -1);
	        $start = array_search($bodyId,$list) ;
	        $length = $start + $length ;
	        $start++;
	    }
	    $this->addStat($this->redisPopUsec);
	    return  $this->redis->lRange($this->listMentions.$uid, $start, $length);
	}/*}}}*/

    //判断用户是否被删除所有评论
    public function isUidRemoving($uid)
    {
        return $this->redis->zScore($this->sRemoveUserComment, $uid);
    }
    
	###########  热门日记      #####################
	/**
	 * 获取热门日记列表,倒序    已测试
	 * 不选择开始于长度则默认获取所有
	 * @param $category
	 * @param $start
	 * @param $length
	 */
	public function getHotDiaryList($category, $start = 0, $length = -1 )/*{{{*/
	{
	    return $this->redis->zRevRange($this->sSetHotDiary.$category, $start, $length);
	}/*}}}*/
	
	/**
	 * 增加热门日志  , 已测试
	 */
	public function addHotDiaryList($category, $bodyId, $time = AC::SERVER_TIME)/*{{{*/
	{
	    $this->redis->zAdd($this->sSetHotDiary.$category, $time, $bodyId);
	    return true;
	}/*}}}*/
	
	public function getHotDiaryAll($start = 0, $length = -1 )/*{{{*/
	{
	    return $this->redis->zRevRange($this->sSetHotDiaryAll, $start, $length);
	}/*}}}*/
	
	public function getHotDiaryScore ($bodyId) {
	    return $this->redis->zScore($this->sSetHotDiaryAll, $bodyId);
	}

    public function getTopDiaryScore ($key, $bodyId) {
        return $this->redis->zScore($this->sSetTopicDiarys.$key, $bodyId);
    }
	
	public function getHotDiaries($id, $length = 20)/*{{{*/
	{
	    $list    = $this->redis->zRevRange($this->sSetHotDiaryAll, 0, -1);
	    if ($id > 0){
	        $start = array_search($id, $list) ;
	        $start = $start + 1;
	    }
	    
	    return  array_slice($list, $start, $length);
	}/*}}}*/
	
	public function addHotDiaryAll($bodyId, $time = AC::SERVER_TIME)/*{{{*/
	{
	    $this->redis->zAdd($this->sSetHotDiaryAll, $time, $bodyId);
	
	}/*}}}*/
	public function countHotDiaryAll()/*{{{*/
	{
	   return $this->redis->zCard($this->sSetHotDiaryAll);
	}/*}}}*/
	
	/**
	 * 热门推荐日记
	 * @param unknown $id
	 * @param unknown $unionKeys
	 * @param number $length
	 * @return multitype:
	 */
	public function getHotRecommendDiary ($id, $unionKeys, $length = 5) {
	    if (empty($unionKeys)) {
	        return array();
	    }
	    
	    // 热门日记分类并集
	    $list = $this->redis->sUnion($unionKeys);
	    return  $list;
	}
	
	/**
	 * 删除热门列表 已测试
	 * @param $category key
	 */
	public function delHotDiaryList($category)/*{{{*/
	{
	    $this->redis->delete($this->sSetHotDiary.$category);
	}/*}}}*/
	
	/**
	 * 统计热门日记数  已测试
	 */
	public function countHotDiaryList($category)/*{{{*/
	{
	    return $this->redis->zCard($this->sSetHotDiary.$category);
	}/*}}}*/

    ###########   话题下的热门点滴列表  ##########################
    /**
     * 设置话题下热门点滴列表
     * @param $topicId
     * @param $bodyId
     * @param int $time
     * @return mixed
     */
    public function setTopicHotDiary($topicId, $bodyId, $time = AC::SERVER_TIME)
    {
        $count = (int)$this->getTopicHotDiaryCount($topicId);
        if ($count > 500){
            $list = $this->redis->zRange($this->sSetTopicHotDiarys . $topicId, 0, 0);
            $this->delTopicHotDiaryCache($topicId, $list[0]);
        }
        $result = $this->redis->zAdd($this->sSetTopicHotDiarys . $topicId, $time, $bodyId);
        $this->addKeysCount($this->sKeysTopicHotDiarys, $topicId);
        return $result;
    }

    /**
     * 获取话题下热门点滴列表
     * @param $topicId
     * @param $start
     * @param int $length
     * @return mixed
     */
    public function getTopicHotDiary($topicId, $start, $length=-1)
    {
        return $this->redis->zRevRange($this->sSetTopicHotDiarys . $topicId, $start, $length);
    }

    /**
     * 获取话题下所有热门点滴
     * @param $topicId
     * @return mixed
     */
    public function getTopicHotDiaryAll($topicId)
    {
        return $this->redis->zRevRange($this->sSetTopicHotDiarys . $topicId, 0, -1);
    }

    /**
     * 获取集合中该成员的索引
     * @param $topicId
     * @param $bodyId
     * @return mixed
     */
    public function getTopicHotDiaryRank($topicId, $bodyId)
    {
        return $this->redis->zRank($this->sSetTopicHotDiarys . $topicId, $bodyId);
    }

    /**
     * 获取话题下热门点滴数量
     * @param $topicId
     * @return mixed
     */
    public function getTopicHotDiaryCount($topicId)
    {
        return $this->redis->zCard($this->sSetTopicHotDiarys . $topicId);
    }

    /**
     * 删除话题key
     * @param $topicId
     * @return mixed
     */
    public function delTopicHotDiaryKey($topicId) {
        $result =  $this->redis->delete($this->sSetTopicHotDiarys . $topicId);
        $this->delKeysCount($this->sKeysTopicHotDiarys,$topicId);
        return $result;
    }

    /**
     * 删除话题下热门点滴
     * @param $topicId
     * @param $bodyId
     * @return mixed
     */
    public function delTopicHotDiaryCache($topicId, $bodyId) {
        return $this->redis->zRem($this->sSetTopicHotDiarys . $topicId, $bodyId);
    }

    /**
     * 获取所有话题key
     * @param $start
     * @param $length
     * @return mixed
     */
    public function getTopicHotDiaryKeys($start, $length) {
        return $this->redis->zRevRange($this->sKeysTopicHotDiarys, $start, $length);
    }


    ###########好友关系    基本完成##########################
	/**
	 * 获取好友列表,倒序    已测试
	 * 不选择开始于长度则默认获取所有
	 * @param $uid
	 * @param $start
	 * @param $length
	 */
	public function friendsList($uid, $start = 0, $length = -1 )/*{{{*/
	{
	    
		return $this->redis->zRevRange($this->sSetFriends.$uid, $start, $length);
	}/*}}}*/
	
	/**
	 * 快速判断是否已关注
	 * @param $uid 
	 * @param $recuid 推荐列表中用户uid
	 */
	public function checkIsFriend($uid, $recuid)/*{{{*/
	{
		return $this->redis->zScore($this->sSetFriends.$uid, $recuid);
	}/*}}}*/
	
	/**
	 * 获取黑名单列表,倒序    已测试
	 * 不选择开始于长度则默认获取所有
	 * @param $uid
	 * @param $start
	 * @param $length
	 */
	public function blockedList($uid, $start = 0, $length = -1 )/*{{{*/
	{
	    return $this->redis->zRevRange($this->sSetBlocked.$uid, $start, $length);
	}/*}}}*/
	
	/**
	 * 增加好友  , 已测试
	 * @param $uid
	 * @param $fUid
	 * @param $time
	 */
	public function addFriends($uid, $fUid, $time = AC::SERVER_TIME)/*{{{*/
	{
	    $this->redis->zAdd($this->sSetFriends.$uid, $time, $fUid);
	    $this->addKeysCount($this->sKeysFriends, $uid);
	    
	}/*}}}*/
	
	/**
	 * 增加粉丝  , 已测试
	 * @param $uid
	 * @param $fUid
	 * @param $time
	 */
	public function addFans($uid, $fUid, $time = AC::SERVER_TIME)/*{{{*/
	{
	    $this->redis->zAdd($this->sSetFollows.$uid, $time, $fUid);
	    $this->addKeysCount($this->sKeysFollowers, $uid);
	     
	    return true;
	}/*}}}*/
	
	/**
	 * 判断是否关注过我
	 * @param unknown $uid
	 * @param unknown $fUid
	 */
	public function checkIsFans($uid, $fUid){
	    return $this->redis->zScore($this->sSetFollows.$uid, $fUid);
	}
	
	/**
	 * 判断是否被我拉黑
	 * @param unknown $uid
	 * @param unknown $fUid
	 */
	public function checkIsBlocked($uid, $fUid){
	    return $this->redis->zScore($this->sSetBlocked.$uid, $fUid);
	}
	
	/**
	 * 增加黑名单 
	 * @param $uid   
	 * @param $fUid 被拉黑用户
	 * @param $time
	 */
	public function addBlocked($uid, $fUid, $time = AC::SERVER_TIME){
	    $this->redis->zAdd($this->sSetBlocked.$uid, $time, $fUid);
	    $this->addKeysCount($this->sKeysBlocked, $uid);
	    $this->addKeysCount($this->sKeysBlocked, $fUid);
	}
	/**
	 * 删除好友 已测试
	 * @param $uid key
	 */
	public function deleteFriends($uid)/*{{{*/
	{
	    $this->redis->delete($this->sSetFriends.$uid);
	    $this->delKeysCount($this->sKeysFriends,$uid);
	    
	}/*}}}*/
	/**
	 * 删除粉丝 已测试
	 * @param $uid key
	 */
	public function deleteFans($uid)/*{{{*/
	{
	    $this->redis->delete($this->sSetFollows.$uid);
	    $this->delKeysCount($this->sKeysFollowers,$uid);
	}/*}}}*/
	
	/**
	 * 删除好友 已测试
	 * @param $uid key
	 * @param $dUid 需要删除的好友
	 */
	public function delFriends($uid,$dUid)/*{{{*/
	{
	   $this->redis->zRem($this->sSetFriends.$uid, $dUid);
	   $this->redis->zRem($this->sSetFollows.$dUid, $uid);
	}/*}}}*/
	/**
	 * 删除好友 已测试
	 * @param $uid key
	 * @param $dUid 需要删除的黑名单
	 */
	public function delBlocked($uid,$dUid)/*{{{*/
	{
	    $result = $this->redis->zRem($this->sSetBlocked.$uid, $dUid);
	}/*}}}*/
	/**
	 * 清空黑名单 已测试
	 * @param $uid key
	 * @param $dUid 需要删除的黑名单
	 */
	public function deleteBlocked($uid)/*{{{*/
	{
	    $result = $this->redis->delete($this->sSetBlocked.$uid);
	    $this->delKeysCount($this->sKeysBlocked,$uid);
	}/*}}}*/
	
	/**
	 * 获取粉丝列表,倒序   
	 * 不设置开始和长度则默认获取所有
	 * @param $uid
	 * @param $start
	 * @param $length
	 */
	public function fansList($uid, $start = 0, $length = -1 )/*{{{*/
	{
	     
	    return $this->redis->zRevRange($this->sSetFollows.$uid, $start, $length);
	}/*}}}*/
	
	/**
	 * 统计粉丝数  已测试
	 * @param $uid
	 */
	public function countFans($uid)/*{{{*/
	{
	    return $this->redis->zCard($this->sSetFollows.$uid);
	}/*}}}*/
	
	/**
	 * 统计黑名单数  已测试
	 * @param $uid
	 */
	public function countBlockeds($uid)/*{{{*/
	{
	    return $this->redis->zCard($this->sSetBlocked.$uid);
	}/*}}}*/
	
	/**
	 * 统计好友数  已测试
	 * @param $uid
	 */
	public function countFriends($uid)/*{{{*/
	{
	    return $this->redis->zCard($this->sSetFriends.$uid);
	}/*}}}*/
	
	###########日记计数    基本完成#####################
	/**
	 * 增加日记统计
	 * @param $bodyId
	 * @param $type
	 * $type = likes,reposts ,views ,comments ;
	 */
	public function addDiaryCount($bodyId, $type, $num = 1)/*{{{*/
	{
	    $this->redis->hIncrBy($this->hashDiaryCount.$bodyId, $type, $num);
	}/*}}}*/
	/**
	 * 初始化日志统计
	 * @param unknown $bodyId
	 * @param unknown $counts
	 */
	public function initDiaryCount($bodyId,$counts){
	    $this->redis->hMset($this->hashDiaryCount.$bodyId,$counts);
	}
	
	//删除日记计数
	public function delDiaryCount($bodyId){
	    $this->redis->delete($this->hashDiaryCount.$bodyId);
	}
	/**
	 * 获取日记统计
	 * @param $bodyId
	 * @param $type
	 * $type = likes,reposts ,views ,comments ;
	 */
	public function getDiaryCount($bodyId, $type=null)/*{{{*/
	{
	    if (empty($type)){ 
	        return $this->redis->hGetAll($this->hashDiaryCount.$bodyId);
	    }
	    $result = $this->redis->hGet($this->hashDiaryCount.$bodyId, $type);
	    if (!empty($result)){
	        return $result;
	    }
	    return false;
	}/*}}}*/
	
	###########用户计数  基本完成#####################
	
	/**
	* 获取用户某类型计数
	* $type:未读日记数，未读喜欢数，未读评论数,好友数，粉丝数，日记数，喜欢数，评论数等
	* diaryTimes  日记数;
	* @param $uid
	* @param $type
	*/
	public function getUserCount($uid, $type=null)/*{{{*/
	{
	    $this->addStat($this->redisGetUsec);
	    if (empty($type)) {
	        return $this->redis->hGetAll($this->hashUserCount.$uid);
	    }
	    $result = $this->redis->hGet($this->hashUserCount.$uid, $type);
	    if (!empty($result)){
	        
	        return $result;
	    }
	    return false;
	}/*}}}*/
	
	/**
	 * 初始化用户计数
	 * @param unknown $uid
	 * @param unknown $data
	 */
	public function initUserCount($uid,$data){
	    $this->redis->hMset($this->hashUserCount.$uid, $data);
	    $this->redis->expire($this->hashUserCount.$uid,604800);
	    $this->addKeysCount($this->sKeysInitUserCount, $uid);
	}
	
	/**
	* 增加用户计数 已测试
	* $type:未读日记数，未读喜欢数，未读评论数,好友数，粉丝数，日记数，喜欢数，评论数等
	* diaryTimes  日记数;
	* @param $uid
	* @param $type
	* @return true >0 || flase < 0
	*/
	public function addUserCount($uid, $type,$num = 1)/*{{{*/
	{
    	$result = $this->redis->hExists($this->hashUserCount.$uid,$type);
    	if ($result){
    	    return $this->redis->hIncrBy($this->hashUserCount.$uid, $type, $num);
    	}
    	$this->redis->hSet($this->hashUserCount.$uid, $type, $num);
    	$this->redis->expire($this->hashUserCount.$uid,172800);
	    $this->addKeysCount($this->sKeysUserCount, $uid);
    	$this->addStat($this->redisSetUsec);
    	return true;
	}/*}}}*/
	
	/**
	 * 清空用户某个类型的计数  已测试
	 * $type:未读日记数，未读喜欢数，未读评论数
	 * @param $uid
	 * @param $type
	 * @return true >0 || false < 0
	 */
	public function delUserCount($uid, $type)/*{{{*/
	{
	    $result = $this->redis->hDel($this->hashUserCount.$uid, $type);
	    if ($result){
	        return $result;
	    }
	    return false;
	}/*}}}*/
	/**
	 * 删除某用户的缓存,用于初始化
	 * @param unknown $uid
	 * @param unknown $type
	 * @return unknown|boolean
	 */
	public function deleteUserCount($uid)/*{{{*/
	{
	    $this->redis->delete($this->hashUserCount.$uid);
	    
	}/*}}}*/
	
	############  日记缓存   基本完成   ###################
	
	/**
	 * 设置日记列表缓存 已测试
	 * 注意:以bodyId为key,将日记的列表以及附件以json格式存放.
	 * $data数据要包含关于该日记列表需要的一切.
	 * @param $uid
	 * @param $data
	 * @return true
	 */
	public function addDiaryCache($bodyId, $result)/*{{{*/
	{
	    if (empty($bodyId) || empty($result)) return false;
	    $this->redis->hMset($this->hashDiaryCache.$bodyId,$result);
	    $this->redis->expire($this->hashDiaryCache.$bodyId,604800);
	    $this->addStat($this->redisSetUsec);
	    $this->addKeysCount($this->sKeysDiaryCache, $bodyId);
	    return $result;
	}/*}}}*/
	
	// 更新该日记缓存的最后读取时间
	public function updateDiaryTime($bodyId, $result){
	    $this->redis->hMset($this->hashDiaryCache.$bodyId, $result);
	    $this->addKeysCount($this->sKeysDiaryCache, $bodyId);
	}
	
	/**
	 * 返回某篇日记的信息
	 * @param $uid
	 * @param $bodyId
	 * @return array || false
	 */
	public function getDiaryCache($bodyId)/*{{{*/
	{
	    $result = $this->redis->hGetAll($this->hashDiaryCache.$bodyId);
	    
	    if (!empty($result)) {
	        $this->addStat($this->redisHashDiaryHit);
	        return $result;
	    }
	    $this->addStat($this->redisHashDiaryMiss);
	    return false;
	}/*}}}*/
	
	/**
	 * 删除某篇日记缓存
	 * @param $uid
	 * @param $bodyId
	 */
	public function delDiaryCache($bodyId)/*{{{*/
	{
	    $result = $this->redis->delete($this->hashDiaryCache.$bodyId);
	    $this->redis->delete($this->hashDiaryCount.$bodyId);
	   // todo 将addkeyCount 中日记缓存中该bodyid删除
	    $this->delKeysCount($this->sKeysDiaryCache, $bodyId);
        return true;
	}/*}}}*/
	
    #################用户缓存    基本完成########################
    /**
     * 用户信息缓存
     * @param $uid
     * @return boolean
     */
    public function addUserCache($uid, $result)/*{{{*/
    {
        if (empty($uid) || empty($result)) return false;
        $this->redis->hMset($this->hashUserCache.$uid, $result);
        $this->redis->expire($this->hashUserCache.$uid,172800);
        $this->addStat($this->redisSetUsec);
        $this->addKeysCount($this->sKeysUserCache, $uid);
    }/*}}}*/
   
    /**
    * 获取用户信息
    * @param $uid
    * @param $type
    * @return boolean || array
    */
    public function getUserCache($uid)/*{{{*/
    {
        $result = $this->redis->hGetAll($this->hashUserCache.$uid);
        if (empty($result)){
        $this->addStat($this->redisHashUserMiss);
            return false;
        }
        $this->addStat($this->redisHashUserHit);
        return $result;
    }/*}}}*/
    //删除用户信息缓存
    public function delUserCache($uid){
        $this->redis->delete($this->hashUserCache.$uid);
        $this->delKeysCount($this->sKeysUserCache,$uid);
    }
    // 更新用户最后读取时间
    public function updateUserCache($uid, $data){
        $this->redis->hMset($this->hashUserCache.$uid,$data);
        $this->addKeysCount($this->sKeysUserCache, $uid);
    }
    // 更新用户最后读取动态时间
    public function getUserFeedCache($uid){
        $result = $this->redis->hGetAll($this->hashUserFeedStatus.$uid);
        if (empty($result)){
            $this->addStat($this->redisHashUserMiss);
            return false;
        }
        $this->addStat($this->redisHashUserHit);
        return $result;
    }
    // 更新用户最后读取动态时间
    public function updateUserFeedCache($uid, $data){
        $this->redis->hMset($this->hashUserFeedStatus.$uid,$data);
        $this->redis->expire($this->hashUserFeedStatus.$uid, 2592000);
        $this->addKeysCount($this->sKeysUserFeedStatus, $uid);
    }
    // 更新用户最后读取通知时间
    public function getUserFeedMeCache($uid){
        $result = $this->redis->hGetAll($this->hashUserFeedMeStatus.$uid);
        if (empty($result)){
            $this->addStat($this->redisHashUserMiss);
            return false;
        }
        $this->addStat($this->redisHashUserHit);
        return $result;
    }
    // 更新用户最后读取通知时间
    public function updateUserFeedMeCache($uid, $data){
        $this->redis->hMset($this->hashUserFeedMeStatus.$uid,$data);
        $this->redis->expire($this->hashUserFeedMeStatus.$uid, 2592000);
        $this->addKeysCount($this->sKeysUserFeedMeStatus, $uid);
    }
    
    // 获取用户刷新时间缓存
    public function getUserFlushTimeCache($uid){
        $result = $this->redis->hGetAll($this->hashUserFlushTimeCache.$uid);
        if (empty($result)){
            $this->addStat($this->redisHashUserMiss);
            return false;
        }
        $this->addStat($this->redisHashUserHit);
        return $result;
    }
    
    // 更新用户刷新时间缓存
    public function updateUserFlushTimeCache($uid, $data){
        $this->redis->hMset($this->hashUserFlushTimeCache.$uid,$data);
        $this->redis->expire($this->hashUserFlushTimeCache.$uid, 2592000);
        $this->addKeysCount($this->sKeysUserFlushTimeStatus, $uid);
    }
    
    /**
     * 设置用户状态信息
     * @param $uid
     * @return boolean
     */
    public function setUserStatus($uid, $data)/*{{{*/
    {
        if (empty($uid) || empty($data)) return false;
        $this->redis->hMset($this->hashUserStatus.$uid, $data);
        $this->redis->expire($this->hashUserStatus.$uid, 2592000);
        $this->addStat($this->redisSetUsec);
        $this->addKeysCount($this->sKeysUserStatus, $uid);
        return true;
    }/*}}}*/
     
    /**
     * 获取用户状态信息
     * @param $uid
     * @param $type
     * @return boolean || array
     */
    public function getUserStatus($uid)/*{{{*/
    {
        $result = $this->redis->hGetAll($this->hashUserStatus.$uid);
        if (empty($result)){
            $this->addStat($this->redisHashUserMiss);
            return false;
        }
        $this->addStat($this->redisHashUserHit);
        return $result;
    }/*}}}*/
    
    public function getStatisticalErrorCount($field)/*{{{*/
    {
        $result = $this->redis->hGetAll($this->hashStatErrorCount);
        if (empty($result[$field])){
            $this->redis->hMset($this->hashStatErrorCount, array($field => 0));
        }
        return $result;
    }/*}}}*/
    
    public function addStatisticalErrorCount($field, $num){
        return $this->redis->hIncrBy($this->hashStatErrorCount, $field, $num);
    }
    
    ##########    token  缓存          ###########
    public function addToken($uid, $data){
        $this->redis->hMset($this->hashTokenCache.$uid, $data);
        $this->redis->expire($this->hashTokenCache.$uid,172800);
        $this->addKeysCount($this->sKeysToken, $uid);
        return true;
    }
    
    public function delToken($uid){
    	$this->redis->delete($this->hashTokenCache.$uid);
    	return true;
    }
    
    public function getToken($uid){
        $result = $this->redis->hGetAll($this->hashTokenCache.$uid);
        if (!empty($result)){
            $this->addStat($this->redisHashTokenHit);
            return $result;
        }
        $this->addStat($this->redisHashTokenMiss);
        return false;
    }
    public function updateTokenTime($uid, $data){
        $this->redis->hMset($this->hashTokenCache.$uid, $data);
        $this->addKeysCount($this->sKeysToken, $uid);
    }
    
    ############# 群+用户 ################
    // 用户加入的群组列表
    public function addOneToUserChatGroups($gid, $uid, $time)
    {
        $result = $this->redis->zAdd($this->sSetUserChatGroups.$uid, $time, $gid);
        // 统计上面使用的key数量
        $this->redis->zAdd($this->sSetUserChatGroupKeys, AC::SERVER_TIME, $uid);
        return $result;
    }
    // 删除用户加入的某个群组
    public function delOneFromUserChatGroups($gid, $uid)
    {
        return $this->redis->zRem($this->sSetUserChatGroups.$uid, $gid);
    }

    // 获取用户加入的群组列表
    public function getUserChatGroupList($uid, $start, $end)
    {
        return $this->redis->zRevRange($this->sSetUserChatGroups.$uid, $start, $end);
    }

    # 往群中添加用户
    public function addUserToChatGroup($gid, $time, $uid)/*{{{*/
    {
        $result = $this->redis->zAdd($this->sSetChatGroup.$gid, $time, $uid);
        $this->redis->set($this->sSetChatGroup, AC::SERVER_TIME);
        return $result;
    }/*}}}*/
    
    # 群中用户的列表
    public function getChatGroupUsersCacheList($gid, $start=0, $end= -1)/*{{{*/
    {
        return $this->redis->zRevRange($this->sSetChatGroup.$gid, $start, $end);
    }/*}}}*/
    
    # 清除群中所有用户
    public function delChatGroupAllUsers($gid)/*{{{*/
    {
        return $this->redis->delete($this->sSetChatGroup.$gid);
    }/*}}}*/
    
    # 删除群中的用户
    public function delChatGroupUserCache($gid, $uid)/*{{{*/
    {
        $result = $this->redis->zRem($this->sSetChatGroup.$gid, $uid);
        return $result;
    }/*}}}*/
    # 暂停圈子
    public function pauseGroup($gid, $time) {
        return $this->redis->zAdd($this->sSetGroupPause, $time, $gid);
    }
    # 暂停圈子列表
    public function getPauseGroupList(){
        return $this->redis->zRange($this->sSetGroupPause, 0, -1);
    }
    # 删除过期圈子
    public function deletePauseGroup($time=AC::SERVER_TIME) {
        return $this->redis->zRemRangeByScore($this->sSetGroupPause, 0, $time);
    }
    # 查看圈子封禁时间
    public function getPauseGroup($gid){
        return $this->redis->zScore($this->sSetGroupPause, $gid);
    }
    
    // 设置聊天记录缓存
    public function setGroupChatMessageCache($gid, $id, $data){
        if (empty($gid) || empty($id) || empty($data)) return false;
        $key = $gid.'_'.$id;
        $this->redis->hMset($this->hashChatMessageCache.$key, $data);
        $this->redis->expire($this->hashChatMessageCache.$key, 86400);
        $this->addKeysCount($this->sSetGroupChatMessage, $key);
        return TRUE;
    }
    
    // 获取缓存详情
    public function getGroupChatMessageCache($gid, $id){
        if (empty($gid) || empty($id)) return false;
        $key = $gid.'_'.$id;
        return $this->redis->hGetAll($this->hashChatMessageCache.$key);
    }
    
    // 添加聊天记录id到列表中去
    public function addGroupChatIdToList($gid, $id){
        $this->redis->rPush($this->listGroupChat.$gid, $id);
        $this->redis->lTrim($this->listGroupChat.$gid, 0, 200);
        $this->addKeysCount($this->sKeysChatGroup, $gid);
        return true;
    }
    
    // 返回聊天记录
    public function getGroupChatList($gid, $id = 0, $start = 0, $length = 19){
        $id     =    intval($id);
        $list   =    $this->redis->lRange($this->listGroupChat.$gid, 0, -1);
        if ($id > 0){
            if (!in_array($id, $list)){
                return array();
            }
            $start = array_search($id, $list);
            $start++;
        }
        return  array_slice($list, $start, -$length);
    }
    
    // 删除列表
    public function delGroupChatIdList($gid){
        $this->redis->delete($this->listGroupChat.$gid);
        return true;
    }
    
    // 获取
    public function getChatListCacheCtime($gid){
        return $this->redis->hGetAll($this->chatListCtimeCache.$gid);
    }
    
    // 设置聊天列表缓存初始化时间
    public function setChatListCacheCtime($gid, $value){
        $this->redis->hMset($this->chatListCtimeCache.$gid, $value);
        $this->redis->expire($this->chatListCtimeCache.$gid,172800);
        $this->addKeysCount($this->sKeysChatListCtime, $gid);
    }
    
    #############聊天室################
    
    // 根据类型获取聊天室列表
    public function getChatRoomListByCategory ($category, $start, $end) {
        return $this->redis->zRevRangeByScore($this->sSetChatRoomCategory.$category, $start, $end);
    }
    
    // 聊天室进分类列表
    public function addChatRoomToCategoryList ($rid, $category , $time = AC::SERVER_TIME) {
        $this->redis->zAdd($this->sSetChatRoomCategory.$category, $time, $rid);
        $this->addKeysCount($this->sKeysChatRoomCategory, $category);
        return true;
    }
    
    // 从分类列表中删除聊天室
    public function delChatRoomFromCategoryList ($rid, $category) {
        return $this->redis->zRem($this->sSetChatRoomCategory.$category, $rid);
    }
    
    // 设置聊天室信息
    public function setChatRoomData ($rid, $data) {
        $this->redis->hMset($this->hashChatRoom.$rid, $data);
        $this->redis->expire($this->hashChatRoom.$rid, AC::DEFAULT_CHAT_ROOM_EXPIRE);
        $this->addKeysCount($this->sKeysChatRoom, $rid);
    }
    
    // 获取聊天室信息
    public function getChatRoomData ($rid) {
        return $this->redis->hGetAll($this->hashChatRoom.$rid);
    }
    
    // 删除聊天室
    public function delChatRoomData ($rid) {
        return $this->redis->delete($this->hashChatRoom.$rid);
    }
    
    // 更新聊天室计数
    public function updateChatRoomMembers ($rid, $field, $num = 1) {
        return $this->redis->hIncrBy($this->hashChatRoom.$rid, $field, $num);
    }
    
    // 添加用户到聊天室成员列表
    public function addUserToChatRoom ($rid, $uid, $time) {
        $result = $this->redis->zAdd($this->sSetChatRoom.$rid, $time, $uid);
        $this->redis->expire($this->sSetChatRoom.$rid, AC::DEFAULT_CHAT_ROOM_EXPIRE);
        return $result;
    }
    
    // 获取聊天室用户列表
    public function getUsersFromChatRoom ($rid) {
        return $this->redis->zRevRange($this->sSetChatRoom.$rid, 0, -1);
    }
    
    public function getChatRoomUserCount ($rid) {
        return $this->redis->zCount($this->sSetChatRoom.$rid, 0, 999999999999);
    }
    
    // 判断用户是否在聊天室中
    public function getUserScoreFromChatRoom ($rid, $uid) {
        return $this->redis->zScore($this->sSetChatRoom.$rid, $uid);
    }
    
    // 更新聊天室用户活跃时间
    public function setUserActive ($rid, $uid, $time) {
        return $this->redis->zAdd($this->sSetChatRoom.$rid, $time, $uid);
    }
    
    // 从聊天室中移除用户
    public function delUserFromChatRoom ($rid, $uid) {
        return $this->redis->zRem($this->sSetChatRoom.$rid, $uid);
    }
    
    // 删除聊天室用户列表
    public function delChatRoomSet ($rid) {
        return $this->redis->delete($this->sSetChatRoom.$rid);
    }
    
    ############# LBS ################
    
    /**
     * 增加城市Id
     */
    public function addCityIdCache($data){
        $result=$this->redis->hMset($this->hashCity,$data);
        $this->addKeysCount($this->sKeysCity,$this->hashCity);
    }
    
    /**
     * 获取城市Id
     */
    public function  getCityIdCache($hash){
        $result=$this->redis->hMget($this->hashCity,$hash);
        return $result;
    }
   
    /**
     * 增加GEO缓存
     */
    public function addCityCache($key,$value,$time){
        $result=$this->redis->zAdd($this->sSetDiaryiesOfCity.$key, $time, $value);
        $this->addKeysCount($this->sKeysDiariesOfCity,$key);

        $lists = $this->redis->zRevRange($this->sSetDiaryiesOfCity.$key, 0, -1);
        if (count($lists) > AC::LIMITLENGTHOFLIST) {
            $score = $this->redis->zScore($this->sSetDiaryiesOfCity.$key, $lists[AC::LIMITLENGTHOFLIST]);
            $this->redis->zRemRangeByScore($this->sSetDiaryiesOfCity.$key, 0, $score);
        }
    }
    /**
     *获取GEO缓存
     */
    public function getCityCache($key,$id,$length){
        $lists=$this->redis->zRevRange($this->sSetDiaryiesOfCity.$key, 0,-1);
              
         $start=0;
         if (intval($id) > 0){
            $start = array_search($id, $lists);
            $start ++;
        }
        $result=array_slice($lists, $start, $length);

        return  $result;  
    }
    /**
     * 删除GEO缓存
     */
    public function removeCityCache($key, $value){
        $this->redis->zDelete($this->sSetDiaryiesOfCity.$key,$value);
    }
    
    // ======================= 话题点滴列表 =====================
    /**
     * 获取点滴话题hash
     * @param unknown $id
     */
    public function getDiaryTopicHash($id){
    	return $this->getHash($this->hashDiaryTopic, $id);
    }
    
    /**
     * 增加点滴话题hahs
     * @param unknown $id
     * @param unknown $data
     */
    public function addDiaryTopicHash($id, $data){
    	 return $this->addHash($this->hashDiaryTopic, $this->sSetDiaryTopic, $id, $data);
    }
    
    /**
     * 获取一个官方点滴话题热度
     * @param unknown $tid
     * @return unknown
     */
    public function getDiaryTopicOfficialHot($tid){
    	$result = $this->redis->zScore($this->sSetHotOfficialTopics,$tid);
    	return $result;
    }
    
    /**
     * 将话题添加官方列表中
     * @param unknown $id
     * @param unknown $score
     */
    public function addDiaryTopicToOfficialSet ($id, $score) {
        return $this->redis->zAdd($this->sSetHotOfficialTopics, $score, $id);
    }

    /**
     * 获取所有热门点滴话题（人为干预的）
     * @return unknown
     */
    public function getDiaryTopicOfficialByHot(){
    	$lists=$this->redis->zRevRange($this->sSetHotOfficialTopics, 0, -1, true);
    	return $lists;
    }

    /**
     * 返回热门点滴话题的排序
     * @param $gid
     * @return mixed
     */
    public function getDiaryTopicRank($gid){
        $result = $this->redis->zrevrank($this->sSetHotOfficialTopics, $gid);
        return $result;
    }

    /**
     * 获取热门点滴话题列表（人为干预的）
     * @param $start
     * @param int $length
     * @return mixed
     */
    public function getDiaryTopicCache($start, $length = 20)
    {
        return $this->redis->zRevRange($this->sSetHotOfficialTopics, $start, $length, true);
    }


    /**
     * 删除点滴话题（非人为干预的）   
     * @return unknown
     */
    public function deleteDiaryTopicHot($tid){
    	$this->redis->zDelete($this->sSetHotTopics,$tid);
    }
    /**
     * 获取所有点滴话题热度（非人为干预的）
     * @param unknown $start
     * @param unknown $length
     * @return unknown
     */
    public function getDiaryTopicByHot(){
    	$lists = $this->redis->zRevRange($this->sSetHotTopics, 0, -1, true);   
        return  $lists;
    }
	/**
	 * 获取一个点滴话题热度（非人为干预的）
	 * @param unknown $tid
	 * @return unknown
	 */
    public function getDiaryTopicHot($tid){
    	$result = $this->redis->zScore($this->sSetHotTopics,$tid);
    	return $result;
    }

    /**
     * 添加话题点滴进列表
     * @param unknown $key
     * @param unknown $value
     * @param unknown $time
     */
    public function addTopicDiaryToList($key,$value,$time){
        $result=$this->redis->zAdd($this->sSetTopicDiarys.$key, $time, $value);
        $this->addKeysCount($this->sKeysTopicDiarys,$key);
        
        $lists = $this->redis->zRevRange($this->sSetTopicDiarys.$key, 0, -1);
        if (count($lists) > AC::LIMITLENGTHOFLIST) {
            $score = $this->redis->zScore($this->sSetTopicDiarys.$key, $lists[AC::LIMITLENGTHOFLIST]);
            $this->redis->zRemRangeByScore($this->sSetTopicDiarys.$key, 0, $score);
        }
        
        return $result;
    }
    
    /**
     * 从列表中删除点滴
     * @param unknown $key
     * @param unknown $value
     */
    public function removeTopicDiaryOfList($key, $value){
        return $this->redis->zDelete($this->sSetTopicDiarys.$key,$value);
    }
    
    /**
     * 获取点滴话题列表
     * @param unknown $key
     * @param unknown $id
     * @param unknown $length
     * @return multitype:
     */
    public function getDiarysByTopicId($key,$id,$length){
        $lists=$this->redis->zRevRange($this->sSetTopicDiarys.$key, 0, -1);
    
        $start=0;
        if (intval($id) > 0){
            $start = array_search($id, $lists);
            $start ++;
        }
        $result = array_slice($lists, $start, $length);
    
        return  $result;
    }
    
    #########运维统计  基本完成##########
    /**
     * 运维统计
     * @param $type
     * $type:
     * redis-timeout
     * redis-push-usec
     * redis-pop-usec
     * redis-get-usec
     * redis-set-usec
     * redis-ltrim-usec
     * redis-llen-usec
     * timeline-hit
     * timeline-miss
     * hash-diary-hit
     * hash-diary-miss
     * hash-user-hit
     * hash-user-miss
     * hash-token-hit
     * hash-token-miss
     * hash-comment-hit
     * hash-comment-miss
     */
    public function addStat($type)/*{{{*/
    {
        return null;
        $this->redis->hIncrBy($this->operationCount.date("Ymd",AC::SERVER_TIME), $type, 1);
    }/*}}}*/
    
    
    ########### keys 统计 ##################
   // 添加统计
    public function addKeysCount($keys, $id){
        $this->redis->zAdd($keys, AC::SERVER_TIME, $id);
    }
    // 删除日记,用户,等信息时删除对应的统计
    public function delKeysCount($keys,$id){
        $this->redis->zRem($keys,$id);
    }
    ######## 重建时间线  ##########
    
    public function addTmpTimeline($uid, $bodyId ,$time){
        $this->redis->zAdd("SSet:tmp:timline:".$uid, $time, $bodyId);
    }
    
    public function getTmpTimeline($uid, $start=0, $length=799){
        return $this->redis->zRevRange("SSet:tmp:timline:".$uid, $start, $length);
    }
    public function delTmpTimeline($uid){
        $this->redis->delete("SSet:tmp:timline:".$uid);
    }
    
    ###########  禁言用户  ################
    public function getShutUpUserCache($uid){
        return $this->redis->hGetAll($this->hashShutUpUserCache.$uid);
    }
    ########### end 禁言    ##################
    
    
    
    /**
     * 后台管理推荐用户
     */
    # 添加推荐的用户
    public function addRecommentUser($type, $time, $uid)/*{{{*/
    {
    	$result = $this->redis->zAdd($this->sSetRecomment.$type, $time, $uid);
    	if ($type > 0)
    	{
    		$result = $this->redis->zAdd($this->sSetRecomment. 0, $time, $uid);
    	}
    	# 添加更新时间
    	$this->redis->set($this->sSetRecomTime, AC::SERVER_TIME);
    	return $result;
    }/*}}}*/
    
    # 推荐用户列表
    public function recommentUserList($type, $start=0, $end= -1)/*{{{*/
    {
    	return $this->redis->zRevRange($this->sSetRecomment.$type, $start, $end);
    }/*}}}*/
    
    # 删除推荐用户
    public function delRecommentUser($type, $uid)/*{{{*/
    {
    	$result = $this->redis->zRem($this->sSetRecomment.$type, $uid);
    	$result = $this->redis->zRem($this->sSetRecomment. 0, $uid);
    	return $result;
    }/*}}}*/
    
    # 统计各类推荐用户
    public function countRecommentUsers($type)/*{{{*/
    {
    	return $this->redis->zCard($this->sSetRecomment.$type);
    }/*}}}*/
    
    # 删除某类用户
    public function delTypeRecommentUser($type, $start=0, $stop= -1)/*{{{*/
    {
    	return $this->redis->zRemRangeByRank($this->sSetRecomment.$type, $start, $stop);
    }/*}}}*/
    
    # 获取更新时间
    public function getRecomTime()/*{{{*/
    {
    	return $this->redis->get($this->sSetRecomTime);
    }
    
    # 获取用户score，判断是否存在
    public function getUserScore($uid)/*{{{*/
    {
    	$result = $this->redis->zScore($this->sSetRecomment. 0, $uid);
    	if ($result)
    	{
    		return 1;
    	}else{
    		return 0;
    	}
    }
    
    /**
     * 设置榜单活跃用户
     * @param unknown $uid
     * @return unknown
     */
    public function setBoardUids($type = 'active', $uid){
        $result = $this->redis->zAdd($this->sSetBoardUsers.$type, AC::SERVER_TIME, $uid);
        $this->redis->set($this->sSetBoardActiveTime, AC::SERVER_TIME);
        return $result;
    }/*}}}*/
    
    /**
     * 获取榜单活跃用户
     * @param unknown $type
     * @param number $start
     * @param unknown $end
     */
    public function getBoardUids($type = 'active', $start=0, $end= -1){
        return $this->redis->zRevRange($this->sSetBoardUsers.$type, $start, $end);
    }

    /**
     * 添加首页机器人消息
     * @param $id
     * @param $data
     */
    public function addRobotMsgHash($id, $data){
        return $this->addHash($this->hashRobotMsgCache, $this->sSetRobotMsg,$id, [json_encode($data)]);
    }

    /**
     * 获取首页机器人消息
     */
    public function getRobotMsgHash(){
        $result = $this->redis->hGetAll($this->hashRobotMsgCache.'list');
        if (empty($result))return [];
        $result = json_decode($result[0], true);
        return $result;
    }

    public function setGuestFindAccountWithEmailCount($email, $count){
        $key = $this->sKeysGuestFindAccount.date('m-d').':'.$email;
        return $this->redis->set($key, $count, 3600*24);
    }

    public function getGuestFindAccountWithEmailCount($email){
        $key = $this->sKeysGuestFindAccount.date('m-d').':'.$email;
        $result = $this->redis->get($key);
        if (empty($result))return 0;
        return (int)$result;
    }


   public function getPlannerCategoryCacheList($type){
       $plannerKey = $this->sKeysPlannerCache.$type;
        return $this->redis->zRange($plannerKey,0,-1);
   }

   public function addPlannerCategoryCache($type, $cid){
        if (empty($cid)) return false;
        $score = AC::SERVER_TIME;
        $plannerKey = $this->sKeysPlannerCache.$type;

        $res = $this->redis->zAdd($plannerKey, $score, $cid);
        $this->addKeysCount($this->sKeysPlannerCount, $plannerKey);
        return $res;
    }

    //添加精选话题
    public function addRecommendTopic($tid ,$time = AC::SERVER_TIME){
        $count = (int)$this->redis->zCard($this->sSetRecommendTopic);
        if ($count > 300){
            $list = $this->redis->zRange($this->sSetRecommendTopic, 0, 0);
            $this->delRecommendTopic($list[0]);
        }
        return $this->redis->zAdd($this->sSetRecommendTopic, $time, $tid);
    }

    public function getRecommendTopicList($start = 0, $length = -1 ){
        return $this->redis->zRevRange($this->sSetRecommendTopic, $start, $length);
    }

    public function delRecommendTopic($tid){
        return $this->redis->zRem($this->sSetRecommendTopic, $tid);
    }

    public function removeRecommendTopic(){
        return $this->redis->delete($this->sSetRecommendTopic);
    }

    //按类型添加热门话题
    public function setHotTypeTopics($type, $value){
        return $this->redis->set($this->sSetHotTypeTopic.$type, $value, 3600);
    }

    public function getHotTypeTopics($type){
        return $this->redis->get($this->sSetHotTypeTopic.$type);
    }

    public function delHotTypeTopics($type)
    {
        return $this->redis->delete($this->sSetHotTypeTopic . $type);
    } 

    public function getLoginFFIDAllowList(){
        return $this->redis->zRevRange('SSet:loginffid', 0, -1);
    }
    
    //添加为你推荐
    public function addRecommendDianDiForU($bodyid ,$time = AC::SERVER_TIME){
        $count = (int)$this->redis->zCard($this->sSetRecomendDianDiForU);
        if ($count > 300){
            $list = $this->redis->zRange($this->sSetRecomendDianDiForU, 0, 0);
            $this->delRecommendTopic($list[0]);
        }
        return $this->redis->zAdd($this->sSetRecomendDianDiForU, $time, $bodyid);
    }

    public function getRecommendDianDiForUList($start = 0, $length = -1 ){
        return $this->redis->zRevRange($this->sSetRecomendDianDiForU, $start, $length, true);
    }

    public function delRecommendDianDiForU($bodyid){
        return $this->redis->zRem($this->sSetRecomendDianDiForU, $bodyid);
    }

    public function removeRecommendDianDiForU(){
        return $this->redis->delete($this->sSetRecomendDianDiForU);
    }

    public function getPlannerCacheByType($key){
        $stringKey = $this->planner[$key];
        $res = $this->redis->get($stringKey);
        return $res;
    }

    public function setPlannerCacheByType($key,$data){
        $stringKey = $this->planner[$key];
        $res = $this->redis->set($stringKey, $data);
        return $res;
    }

    public function delPlannerCacheByType($key){
        $stringKey = $this->planner[$key];
        $res = $this->redis->delete($stringKey);
        return $res;
    }

    /**
     * 获取匿名最新列表
     * @param $category
     * @param string $bodyId
     * @param string $start
     * @param string $length
     * @return array
     */
    public function getAnonymousTimeline($category, $bodyId='0', $start='0', $length='49'){
        $bodyId = intval($bodyId);
        $list   =    $this->redis->lRange($this->listAnonymous.$category, 0, -1);
        if ($bodyId > 0){
            $start = array_search($bodyId,$list) ;
            $start ++;
        }
        return  array_slice($list, $start, $length);
    }

    /**
     * 删除广场最新的时间线
     * @param unknown $label
     * @param unknown $bodyId
     */
    public function delAnonymousTimeline($category, $bodyId){
        $this->redis->lRem($this->listAnonymous.$category,$bodyId,0);
        return true;
    }

    /**
     * 增加点滴热门评论缓存
     * @param $bodyId
     * @return boolean
     */
    public function addHotComment($bodyId, $weight, $commentId)/*{{{*/
    {
        if (empty($bodyId) || empty($weight) || empty($commentId)) return false;
        $result = $this->redis->zAdd($this->hashHotComment.$bodyId, $weight, $commentId);
        $this->redis->expire($this->hashHotComment.$bodyId,240);
        $this->redis->zAdd($this->sSetHotCommentList, AC::SERVER_TIME, $bodyId);
        return $result;
    }/*}}}*/

    public function delHotComment($bodyId, $commentId){
        return $this->redis->zRem($this->hashHotComment.$bodyId, $commentId);
    }
   
    /**
    * 获取点滴热门评论缓存
    * @param $bodyId
    * @param $type
    * @return boolean || array
    */
    public function getHotComment($bodyId, $start = 0, $length = 20)/*{{{*/
    {
        $result = $this->redis->zRevRange($this->hashHotComment.$bodyId, $start, $length);
        return $result;
    }/*}}}*/

    /**
     * 加入匿名最新列表
     * @param $category
     * @param $bodyId
     */
    public function addAonoymoousTimeline($category, $bodyId){
        $this->redis->lPush($this->listAnonymous.$category,$bodyId);
        $this->redis->lTrim($this->listAnonymous.$category,0,4999);
    }

    //匿名推荐
    public function addRecommendAnonymousList($bodyid ,$time = AC::SERVER_TIME){
        $count = (int)$this->redis->zCard($this->sSetRecomendAnonymous);
        if ($count > 300){
            $list = $this->redis->zRange($this->sSetRecomendAnonymous, 0, 0);
            $this->delRecommendAnonymousList($list[0]);
        }
        return $this->redis->zAdd($this->sSetRecomendAnonymous, $time, $bodyid);
    }

    public function getStringByKey($key){
        return $this->redis->get('gs:'.$key);
    }

    public function addStringByKey($key, $data, $ttl=259200){
        $this->redis->set('gs:'.$key, $data);
        $this->redis->expire('gs:'.$key, $ttl);
        return true;
    }

    public function removeStringByKey($key){
        return $this->redis->delete('gs:'.$key);
    }

    public function getRecommendAnonymousList($start=0, $len=-1){
        return $this->redis->zRevRange($this->sSetRecomendAnonymous, $start, $len, true);
    }

    public function delRecommendAnonymousList($bodyid){
        return $this->redis->zRem($this->sSetRecomendAnonymous, $bodyid);
    }

    public function removeRecommendAnonymousList(){
        return $this->redis->delete($this->sSetRecomendAnonymous);
    }

    public function getOfficialAnonymousList($start=0, $len=-1){
        return $this->redis->zRevRange($this->sSetOfficialAnonymous, $start, $len, true);
    }

    public function delOfficialAnonymousList($bodyid){
        return $this->redis->zRem($this->sSetOfficialAnonymous, $bodyid);
    }

    public function addOfficialAnonymousList($bodyid ,$time = AC::SERVER_TIME)
    {
        $count = (int)$this->redis->zCard($this->sSetOfficialAnonymous);
        if ($count > 10) {
            $list = $this->redis->zRange($this->sSetOfficialAnonymous, 0, 0);
            $this->delOfficialAnonymousList($list[0]);
        }
        return $this->redis->zAdd($this->sSetOfficialAnonymous, $time, $bodyid);
    }

    public function getAnonyCheckConnent($uid, $bodyid)
    {
        return $this->redis->get($this->sSetCheckAnonyComment.$bodyid.$uid);
    }

    public function setAnonyCheckConnent($uid, $bodyid, $content)
    {
        return $this->redis->set($this->sSetCheckAnonyComment.$bodyid.$uid, $content);
    }

    //添加关注话题
    public function addUserFollowTopic($uid, $gid ,$time = AC::SERVER_TIME){
        $count = (int)$this->redis->zCard($this->sSetFollowTopic.$uid);
        if ($count > AC::FOLLOW_TOPIC_UPPER_LIMIT){
            $list = $this->redis->zRange($this->sSetFollowTopic.$uid, 0, 0);
            $this->delRecommendTopic($list[0]);
        }
        return $this->redis->zAdd($this->sSetFollowTopic.$uid, $time, $gid);
    }

    public function getUserFollowTopicList($uid, $start = 0, $length = -1 ){
        return $this->redis->zRevRange($this->sSetFollowTopic.$uid, $start, $length, true);
    }

    public function delUserFollowTopic($uid, $gid){
        return $this->redis->zRem($this->sSetFollowTopic.$uid, $gid);
    }
    public function removeUserFollowTopic($uid){
        return $this->redis->delete($this->sSetFollowTopic.$uid);
    }

    public function addUserViewTopicInfo($uid, $tid, $time = AC::SERVER_TIME){
        return $this->redis->zAdd($this->sSetViewTopic.$uid, $time, $tid);
    }
    public function getUserViewTopic($uid, $start = 0, $length = -1){
        $result = $this->redis->zRevRange($this->sSetViewTopic.$uid, $start, $length);
        return $result;
    }
    /**
     * 获取hash信息
     * @param $hash_key
     * @return mixed
     */
    public function getHashCache($hash_key)
    {
        return $this->redis->hGetAll($hash_key);
    }

    /**
     * 设置hash信息、加统计及过期时间
     * @param $hash_key
     * @param $sset_key
     * @param $expire
     * @param $data
     */
    public function setHashCache($hash_key, $sset_key, $expire, $data, $key)
    {
        $this->redis->hMset($hash_key, $data);
        if ($expire) {
            $this->redis->expire($hash_key, $expire);
        }
        $this->addKeysCount($sset_key, $key);
    }

    /**
     * 删除hash数据
     * @param $hash_key
     * @param $sset_key
     * @return mixed
     */
    public function detHashCache($hash_key, $sset_key)
    {
        $this->redis->delete($hash_key);
        return $this->redis->zRem($sset_key, $hash_key);
    }
    
    public  function getShopDisguise(){
        return $this->redis->get($this->setDisguise);
    }

    public function setShopDisguise($data){
        return $this->redis->set($this->setDisguise,$data);
    }

    ###############  订阅号  start  ################

    public function getSubscriptionCache($start, $length = 20)
    {
        return $this->redis->zRevRange($this->sSetSubscription, $start, $length, true);
    }
    public function findSubscription($uid){
        return $this->redis->zScore($this->sSetSubscription, $uid);
    }
    public function setSubscriptionUids($uid, $sort=AC::SERVER_TIME){
        $result = $this->redis->zAdd($this->sSetSubscription, $sort, $uid);
        return $result;
    }
    public function delSubscription (){
        $result = $this->redis->delete($this->sSetSubscription);
        return $result;
    }
    //返回达人用户在集合中的下标，有序集合成员按分数值递减(从大到小)排序
    public function getSubscriptionRank($uid){
        $result = $this->redis->zrevrank($this->sSetSubscription, $uid);
        return $result;
    }

    ###############  会员屏蔽  start  ################

    /**
     * 增加屏蔽用户id
     * @param $uid
     * @param $shieldUid
     * @param int $score
     * @return mixed
     */
    public function setUserShieldList($uid, $shieldUid, $score=AC::SERVER_TIME){
        $result = $this->redis->zAdd($this->sSetShieldList . $uid, $score, $shieldUid);
        return $result;
    }

    /**
     * 返回有效屏蔽用户的数量
     * @param $uid
     * @param $min
     * @param $max
     * @return mixed
     */
    public function getUserShieldCount($uid, $min, $max){
        $result = $this->redis->zCOUNT($this->sSetShieldList . $uid, $min, $max);
        return $result;
    }

    /**
     * 返回被屏蔽用户的开始时间
     * @param $uid
     * @param $shieldUid
     * @return mixed
     */
    public function findUserShieldList($uid, $shieldUid){
        return $this->redis->zScore($this->sSetShieldList . $uid, $shieldUid);
    }

    /**
     * 返回屏蔽列表（带分数）
     * @param $uid
     * @param $withScores
     * @return mixed
     */
    public function getUserShieldList($uid, $withScores = FALSE){
        if($withScores) {
            return $this->redis->zRange($this->sSetShieldList . $uid, 0, -1, 'WITHSCORES');
        }else{
            return $this->redis->zRange($this->sSetShieldList . $uid, 0, -1);
        }
    }

    /**
     * 删除屏蔽列表中某一用户id
     * @param $uid
     * @param $shieldUid
     * @return mixed
     */
    public function removeUserShieldListItem($uid, $shieldUid) {
        return $this->redis->zRem($this->sSetShieldList . $uid, $shieldUid);
    }

    /**
     * 删除列表中时间超过24小时的用户id
     * @param $uid
     * @param $min
     * @param $max
     * @return mixed
     */
    public function removeUserShieldListItemByScore($uid, $min, $max) {
        return $this->redis->zRemRangeByScore($this->sSetShieldList . $uid, $min, $max);
    }

    ###############  会员屏蔽  end  ################

    public function setArticleTimedList($key, $time) {
        return $this->redis->zAdd($this->hashArticleDiaryList, $time, $key);
    }

}